ans = []
n = int(input())


def e(x):
    if x == 0 or x == 1:return 1
    return x * e(x-1)


for i in range(0, n):
    a, b = map(int, input().split())
    ans.append(str(e(a)).count(str(b)))


for i in range(0, n):
    print(ans[i])
